<template>
	<view class="page">
		<cu-custom bgColor="bg-tran" :isBack="false">
			<!-- <block slot="backText">返回</block> -->
			<block slot="content"> </block>
		</cu-custom>
		<view class="card">
			<view class="head-portrait">
				<!-- <image style="width: 100%;height: 100%;" src="../static/imgs/paidaxing.jpg"></image> -->
				<open-data type="userAvatarUrl" class="img"></open-data>
			</view>
			<text class="welcome">{{ tabTitle }}</text>
			<view class="tabs" style="margin-top: 40rpx;">
				<view class="tabs_header">
					<text :class="{ active: tabActive == 'tlogin' }" @click="tabChange('tlogin')">登录</text>
					<text :class="{ active: tabActive == 'slogin' }" @click="tabChange('slogin')">注册</text>
					<!-- <text v-if="tabActive=='wxlogin'" :class="{ active: tabActive == 'wxlogin' }" @click="tabActive = 'wxlogin'">微信登录</text> -->
				</view>
			</view>
			<view>
				<view v-if="tabActive == 'slogin'">
					<view class="input_bg">
						<image src="../static/imgs/icons/phone.png"></image>
						<input v-model="userAccount" maxlength="15" placeholder="phone number" />
					</view>
					<view class="input_bg">
						<image src="../static/imgs/icons/pwd.png"></image>
						<input password v-model="userPassword" maxlength="20" placeholder="password" />
					</view>
					<view class="input_bg">
						<image src="../static/imgs/icons/code.png"></image>
						<input password v-model="userPassword" maxlength="20" placeholder="code" />
						<button class="code_btn" @click="sendCode()" :class="codeTime > 59 ? '':'d' ">
							{{ codeTime > 59 ? 'Send Code' : codeTime + ' S Again' }}
						</button>
					</view>
					<view class="padding flex flex-direction">
						<button class="cu-btn bg-gradual-blue margin-tb-sm lg" @click="login">注  册</button>
					</view>
				</view>
				<view v-else-if="tabActive == 'wxlogin'">
					<!-- <view>
						<image src="../static/imgs/icons/user.png"></image>
						<input v-model="userInfo.userName" disabled />
					</view>
					<button class="btn-wx" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信登录</button> -->
				</view>
				<view v-else-if="tabActive == 'alogin'">
					You Are Super Man
				</view>
				<view v-else>
					<view class="input_bg">
						<image src="../static/imgs/icons/user.png"></image>
						<input v-model="userAccount" maxlength="15" placeholder="account" />
					</view>
					<view class="input_bg">
						<image src="../static/imgs/icons/pwd.png"></image>
						<input password v-model="userPassword" maxlength="20" placeholder="password" />
					</view>
					<view class="padding flex flex-direction">
						<button class="cu-btn bg-gradual-blue margin-tb-sm lg" @click="login">登  录</button>
						<!-- <text class="text-black text-bold" @click="tabChange('alogin')">管理员</text> -->
						<text class="text-black text-bold" @click="toIndex">前往首页</text>
					</view>
				</view>
			</view>
		</view>
		<view class="footer">
			<view class="other_login">
				<!-- <button class="btn-phone" open-type="getUserInfo" @getuserinfo="appLoginWx"></button> -->
				<button class="btn-phone" @click="appLoginWx"></button>
			</view>
			<text class="text-white">WeChat</text>
		</view>
		<view class="cu-load load-modal" v-if="loadModal">
			<!-- <view class="cuIcon-emojifill text-orange"></view> -->
			<image src="/static/imgs/wx_logo.png" mode="aspectFit"></image>
			<view class="text-white">授权中...</view>
		</view>
	</view>
</template>

<script>
	import WXBizDataCrypt from '@/common/WXBizDataCrypt.js'
	export default {
		components: {},
		data() {
			return {
				statusHeight: App.prototype.statusHeight,
				loadModal: false,
				tabActive: 'tlogin',
				tabTitle: '登 录',
				codeTimer: null,
				codeTime: 60,
				form: {
					userAccount: '',
					userPassword: '',
				},
				userInfo: {
					openid: '',
					session_key: '',
					nickName: '',
					avatarUrl: ''
				}
			};
		},
		onLoad() {
			console.log(this.statusHeight)
			let u = uni.getStorageSync('userInfo')
			let f = uni.getStorageSync('autoLogin')
			if (!f) return
			if (u._id) {
				uni.showLoading({
					title:"登录中...",
					mask: true
				})
				console.log(u)
				setTimeout(()=>{
					uni.hideLoading()
					uni.switchTab({
						url: '/pages/index/index'
					})
				},2000)
			}
		},
		methods: {
			toIndex (){
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			loadWxModal(e) {
				this.loadModal = e
			},
			tabChange(type) {
				this.tabActive = type
				if (type === 'tlogin') {
					// this.tabTitle = 'Sign in'
					this.tabTitle = '登 录'
				} else if (type === 'slogin') {
					// this.tabTitle = 'Sign up'
					this.tabTitle = '注 册'
				} else if (type === 'alogin') {
					this.tabTitle = 'Super Man'
				}
			},
			login() {
				uniCloud.callFunction({
						name: 'test',
						data: {
							a: 1
						}
					})
					.then(res => {
						console.log(res)
					});
			},
			login() {
				uni.showToast({
					icon: 'none',
					title: '请使用下方微信登录',
					duration: 3000
				})
			},
			// login() {
			// 	uni.switchTab({
			// 		url: '/pages/index/index'
			// 	})
			// 	return
			// 	let self = this
			// 	if (!this.userAccount || !this.userPassword) {
			// 		uni.showToast({
			// 			title: '请填写账号与密码',
			// 			icon: 'none',
			// 			duration: 2000
			// 		});
			// 		return
			// 	} else {
			// 		let charEnNumReg = /^[A-Za-z0-9]+$/
			// 		console.log(this.userAccount.match(charEnNumReg))
			// 		if (this.userAccount.match(charEnNumReg)) {
			// 			uni.showLoading({
			// 				title: '登录中...'
			// 			})
			// 			this.$http.post('/api/user/user_login', {
			// 					account: this.userAccount,
			// 					password: this.userPassword
			// 				})
			// 				.then(res => {
			// 					if (res.status == 100) {
			// 						console.log(res.data)
			// 						uni.setStorageSync('userInfo', res.data)
			// 						uni.switchTab({
			// 							url: '/pages/index/index'
			// 						})
			// 					} else {
			// 						uni.showToast({
			// 							title: res.message,
			// 							icon: 'none',
			// 							duration: 2000
			// 						});
			// 					}
			// 					uni.hideLoading()
			// 				})
			// 		} else {
			// 			uni.showToast({
			// 				title: '账号只能是字母和数字',
			// 				icon: 'none',
			// 				duration: 2000
			// 			});
			// 		}
			// 	}
			// },
			sendCode() {
				if (this.codeTimer) {
					return
				}
				this.codeTimer = setInterval(() => {
					this.codeTime--
					if (this.codeTime == 0) {
						clearInterval(this.codeTimer)
						this.codeTime = 60
						this.codeTimer = null
					}
				}, 1000)
			},
			appLoginWx() {
				let self = this
				uni.getUserProfile({
					desc: '向小红帽报告一下信息',
					success: userProfile => {
						console.log('微信用户数据', userProfile)
						self.loadWxModal(true)
						let u = userProfile.userInfo
						uni.getProvider({
							service: 'oauth',
							success: wxProvider => {
								console.log('服务供应商', wxProvider)
								if (~wxProvider.provider.indexOf('weixin')) {
									uni.login({
										provider: 'weixin',
										success: wxCodeRes => {
											console.log('微信CODE响应', wxCodeRes)
											// uniCloud.callFunction({
											// 	name: 'wx_login',
											// 	data: { code: wxCodeRes.code },
											// 	success(wxOpenRes) {
											// 	},
											// 	fail(e) {
											// 		self.loadWxModal(false)
											// 		console.log(e)
											// 	}
											// })
											self.$http.get('https://music.tomhub.cn/music/wx/login', { code: wxCodeRes.code }, {tk: 'e920e52fad7edc74287d6fd7f5e56546'}, {})
												.then(wxOpenRes => {
													console.log('微信OPEN响应', wxOpenRes)
													self.userInfo.openid = wxOpenRes.openid
													self.userInfo.session_key = wxOpenRes.session_key
													self.userInfo.nickName = u.nickName
													self.userInfo.avatarUrl = u.avatarUrl
													self.userInfo.gender = u.gender																	
													self.userInfo.createTime = new Date()
													self.userInfo.userPhone = ''
													// 查询数据库是否有该账号，有则登录，无则添加
													uniCloud.callFunction({
														name:'check_user_have',
														data: {
															openid: self.userInfo.openid
														},
														success(r1){
															self.loadWxModal(false)
															if (r1.result.data.length > 0) {
																let user = r1.result.data[0]
																uni.setStorageSync('userInfo', user)
																uni.switchTab({
																	url: '/pages/index/index'
																})
															} else {
																uniCloud.callFunction({
																	name:'add_user',
																	data: self.userInfo,
																	success(r2){
																		console.log('新增用户成功')
																		let id = r2.result.data
																		self.userInfo._id = id
																		uni.setStorageSync('userInfo', self.userInfo)
																		uni.switchTab({
																			url: '/pages/index/index'
																		})
																	},
																	fail(e){ console.log(e) }
																})
															}
														},
														fail(e){ 
															self.loadWxModal(false)
															console.log(e) 
														}
													})
												})
											// let url =
											// 	'https://api.weixin.qq.com/sns/jscode2session?appid=' +
											// 	self.appid + '&secret=' + self.secret + '&js_code=' +
											// 	wxCodeRes.code + '&grant_type=authorization_code'
											// uni.request({ url: url,
											// 	success: wxOpenRes => {
													
											// 	},
											// 	fail: () => {
											// 		self.loadWxModal(false)
											// 		uni.showToast({
											// 			title: "账号数据获取失败",
											// 			icon: "none",
											// 			duration: 2000
											// 		});
											// 	}
											// });
										},
										fail: () => {
											self.loadWxModal(false)
											uni.showToast({
												title: "微信登录失败",
												icon: "none",
												duration: 2000
											});
										}
									})
								} else {
									self.loadWxModal(false)
									uni.showToast({
										title: '请先安装微信或升级版本',
										icon: "none",
										duration: 2000
									});
								}
							}
						});
					},
					fail: () => {
						uni.showToast({
							title: "您取消了授权",
							icon: "none",
							duration: 2000
						})
					}
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.page {
		height: 100vh;
		background-image: url(../static/imgs/login_bg.jpg);
		position: relative;
		overflow: hidden;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;

		.card {
			width: 84%;
			position: absolute;
			top: 32%;
			left: 8%;
			padding: 0 30rpx;
			box-sizing: border-box;
			padding-top: 120rpx;
			transform: translateY(-70rpx);
			overflow: initial;
			border-radius: 20rpx;
			background-color: #FFFFFF80;
			box-shadow: 0rpx 0rpx 30rpx 10rpx rgba(0, 0, 0, 0.5);

			.head-portrait {
				width: 160rpx;
				height: 160rpx;
				margin: 0 auto;
				border-radius: 50%;
				// background-color: #ffffff;
				// background-image: url('https://gitee.com/baby783/angel6_img_bed/raw/master/uni_em/background/logo_min.jpg');
				overflow: hidden;
				position: absolute;
				top: -80rpx;
				left: calc(50% - 80rpx);
				box-shadow: 0rpx 0rpx 20rpx 1rpx rgba(0, 0, 0, 0.2);
			}

			.welcome {
				font-size: 38rpx;
				display: block;
				text-align: center;
				letter-spacing: 3rpx;
				line-height: 1;
				position: relative;
				font-weight: bolder;
			}

			.welcome::before {
				content: ' ';
				position: absolute;
				right: 80rpx;
				top: -30rpx;
				width: 10rpx;
				height: 10rpx;
				border-radius: 50%;
				border: 5rpx solid #008dd3;
			}

			.welcome::after {
				content: ' ';
				position: absolute;
				right: 100rpx;
				bottom: -10rpx;
				width: 20rpx;
				height: 20rpx;
				border-radius: 50%;
				border: 5rpx solid #008dd3;
			}

			.tabs {
				.tabs_header {
					text {
						transition: all .2s;
						display: inline-block;
						padding: 10rpx 20rpx;
						text-align: center;
						font-size: 24rpx;
						height: 60rpx;
						color: #333;
						position: relative;
						letter-spacing: 3rpx;
						font-weight: bold;
					}

					text.active {
						color: #206bb1;
						transform: scale(1.2);
					}

					text::after {
						content: ' ';
						transition: all .3s;
						position: absolute;
						left: calc(50% - 18rpx);
						bottom: 18rpx;
						transform: scale(0);
						width: 30rpx;
						height: 30rpx;
						background-size: 30rpx 20rpx;
						background-repeat: no-repeat;
						background-image: url(/static/imgs/icons/check.png);
					}

					text.active::after {
						width: 38rpx;
						height: 20rpx;
						transform: scale(1);
					}
				}

				.tabs_header.center {
					text-align: center;
				}
			}

			.input_bg {
				height: 70rpx;
				display: flex;
				border-bottom: 5rpx solid #0f3f6a;
				margin: 30rpx 50rpx;

				image {
					flex-shrink: 0;
					width: 50rpx;
					height: 50rpx;
				}

				input {
					font-size: 25rpx;
					height: 70rpx;
					line-height: 70rpx;
					letter-spacing: 1rpx;
					padding: 0 0 10rpx 10rpx;
				}

				.code_btn {
					font-size: 20rpx;
					margin: 0 0 15rpx 0;
					width: 50%;
					background-color: #058077;
					color: white;

					&.d {
						background-color: #cacbcc;
						color: #3e5865;
					}
				}

			}
		}

		.footer {
			position: absolute;
			bottom: 30rpx;
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			align-content: center;
			justify-content: center;

			.other_login {
				padding: 5rpx;
				height: -10rpx;
				position: relative;
				display: inline-block;

				.btn-phone {
					border: none;
					height: 60rpx;
					width: 60rpx;
					background: url(../static/imgs/wx_logo.png);
					background-size: cover;
				}
			}

			.other_login::before {
				content: ' ';
				position: absolute;
				top: 35rpx;
				left: -165rpx;
				width: 150rpx;
				height: 1rpx;
				background-color: #eee;
			}

			.other_login::after {
				content: ' ';
				position: absolute;
				top: 35rpx;
				right: -165rpx;
				width: 150rpx;
				height: 1rpx;
				background-color: #eee;
			}
		}

	}



	// .btn-wx {
	// 	margin: 0 atuo;
	// 	margin-top: 30rpx !important;
	// 	margin-bottom: 30rpx !important;
	// 	height: 80rpx;
	// 	line-height: 80rpx;
	// 	width: 100%;
	// 	padding: 0rpx;
	// 	text-align: center;
	// 	background-color: #00aa7f;
	// 	font-size: 32rpx;
	// 	color: #fff;
	// 	-webkit-border-radius: 16px;
	// 	border-radius: 50rpx;
	// }

	// .btn-reg {
	// 	margin: 0 atuo;
	// 	margin-top: 30rpx !important;
	// 	margin-bottom: 30rpx !important;
	// 	height: 80rpx;
	// 	line-height: 80rpx;
	// 	width: 100%;
	// 	padding: 0rpx;
	// 	text-align: center;
	// 	background-color: #0d8ec5;
	// 	font-size: 32rpx;
	// 	color: #fff;
	// 	-webkit-border-radius: 16px;
	// 	border-radius: 50rpx;
	// }
	.cu-load.load-modal {
		background-color: rgba(68, 15, 15, 0.8);
	}
	.cu-load.load-modal::after {
		background-color: rgba(68, 15, 15, 0);
		border-left: 6rpx solid white;
	}
</style>
